<?php

require_once('lib/generic_search/generic_search.php');
require_once('lib/gymnasium.php');
require_once('lib/category.php');

$search = new SearchHelper();
$search->set_prefix('m');

// init champs de la recherche
$search->def_text_field('name', 'Nom');
$search->def_text_field('firstname', 'Prénom');
$search->def_text_field('address', 'Adresse');
$search->def_text_field('city', 'Ville');
$search->def_text_field('postal_code', 'Code postal');
$search->def_date_field('date_pre_registration', 'Date pré-inscription');
$search->def_date_field('date_registration', 'Date inscription');
$search->def_int_field('computed_age', 'Age', True);
$search->def_text_field('email', 'E-mail');
$search->def_text_field('homephone', 'Téléphone');
$search->def_text_field('cellphone', 'Téléphone portable');

$search->def_options_field('status_id', 'Statut');
$statuses = member_status_list();
while ($row = mysql_fetch_assoc($statuses)) {
	$search->add_option('status_id', $row['id'], $row['label'].' ('.$row['nb_members'].')');
}


$search->def_xor_options_field('idGymnasium', 'Gymnase');
$gyms = gym_get_list();
while ($row = mysql_fetch_assoc($gyms)) {
	$search->add_option('idGymnasium', $row['id'], $row['name']);
}

$search->def_xor_options_field('idCategory', 'Créneau horaire');
$cats = cat_get_list();
while ($row = mysql_fetch_assoc($cats)) {
	$search->add_option('idCategory', $row['id'], $row['name']);
}

$search->def_xor_options_field('gender', 'Sexe');
$search->add_option('gender', 'M', 'Homme');
$search->add_option('gender', 'F', 'Femme');
$search->def_text_field('climbing_level', 'Niveau d\'escalade');
$search->def_int_field('climbing_years_practice', 'Années de pratique');
$search->def_bool_field('competitions', 'Participe aux compétitions');
$search->def_bool_field('has_pass92', 'Titulaire du P@ss92');
$search->def_bool_field('need_registration_proof', 'Demande un justificatif d\'inscription');
$search->def_bool_field('authorize_image_use', 'Autorise l\'utilisation de son image');
$search->def_int_field('connection_count', 'Nombre de connexions');
$search->def_date_field('last_connection', 'Date de dernière connexion');
$search->def_date_field('date_registration_check', 'Date de réception du chèque de règlement');
$search->def_date_field('date_medical_certificate', 'Date de réception du certificat médical');

$search->set_sql_headers('
SELECT m.id, m.name, m.firstname, m.birth, m.date_registration,
c.name AS category_name, g.name AS gymnasium_name,
s.label as status_label, s.color as status_color,
FLOOR(DATEDIFF(
	NOW(),
	CONCAT_WS("-", SUBSTR(m.birth, 7, 4), SUBSTR(m.birth, 4, 2), SUBSTR(m.birth, 1, 2))
	) / 365) as computed_age
FROM Member m
LEFT JOIN Category c on c.id = m.idCategory
LEFT JOIN Gymnasium g on g.id = m.idGymnasium
JOIN MemberStatus s on s.id = m.status_id');

?>
<script src="pages_admin/adherents/adherents.js" type="text/javascript"></script>
<h3 id="top">Recherche avancée</h3>
<?php

if (isset($_POST['search'])) {
	$sql = $search->build_sql_query();

	$data = sql_query($sql);
	$count = mysql_num_rows($data);
	if ($count > 0) {
	?>
	<div class="info">
		<?php echo $count.' '.plural('résultat', $count); ?>
	</div>
	<table class="dataTable">
		<thead>
		<tr>
			<th>Nom</th>
			<th>Prénom</th>
			<th>Date de naissance</th>
			<th>Statut</th>
			<th>Créneau horaire</th>
			<th>Gymnase</th>
			<th>Inscription</th>
		</tr>
		</thead>
		<tbody>
		<?php
		while ($row = mysql_fetch_assoc($data)) {
			echo '
			<tr>
				<td><a href="javascript:details('.$row['id'].')">'.icon_tag('user').'</a> &nbsp;'.$row['name'].'</td>
				<td>'.$row['firstname'].'</td>
				<td>'.$row['birth'].'</td>
				<td><span style="color: '.$row['status_color'].'">'.$row['status_label'].'</span></td>
				<td>'.$row['category_name'].'</td>
				<td>'.$row['gymnasium_name'].'</td>
				<td>'.date_to_str($row['date_registration']).'</td>
			</tr>';
		}
		?>
		</tbody>
	</table>
	<?php
	}
	else {
		?>
		<div class="info">Pas de résultat</div>
		<?php
	}
}
?>
<form method="post" action="<?php echo  $_SERVER['REQUEST_URI'] ?>" style="font-size: 11px">
	<?php $search->show_form($_SERVER['REQUEST_URI']); ?>
	<p class="center">
		<input type="submit" name="search" value="Rechercher" />
	</p>
</form>
<p>
	<?php echo icon_tag('arrow_up') ?><a href="#top">Remonter en haut de page</a>
</p>
